package com.suwadi.web.controller.microfinance;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suwadi.domain.Beneficiary;
import com.suwadi.domain.SavingAccount;
import com.suwadi.service.BeneficiaryService;
import com.suwadi.service.SavingAccountDepositService;
import com.suwadi.service.SavingAccountService;
import com.suwadi.web.pagination.Pager;

@Controller
public class SavingAccountDepositsController {
	private SavingAccountDepositService savingAccountDepositService;
	private BeneficiaryService beneficiaryService;
	private SavingAccountService savingAccountService;

	@Autowired
	public void setSavingAccountDepositService(
			SavingAccountDepositService savingAccountDepositService) {
		this.savingAccountDepositService = savingAccountDepositService;
	}

	@Autowired
	public void setBeneficiaryService(BeneficiaryService beneficiaryService) {
		this.beneficiaryService = beneficiaryService;
	}

	@Autowired
	public void setSavingAccountService(
			SavingAccountService savingAccountService) {
		this.savingAccountService = savingAccountService;
	}

	@RequestMapping(value = "/microfinance/savingAccounts/{id}/savingAccountDeposits/list")
	public String list(@PathVariable("id") Long id, Model model, Pager pager) {
		SavingAccount savingAccount = this.savingAccountService.findById(id,
				"beneficiary");
		Beneficiary beneficiary = this.beneficiaryService.findById(
				savingAccount.getBeneficiary().getId(), "group",
				"group.society", "group.society.gnDivision",
				"group.society.gnDivision.division",
				"group.society.gnDivision.division.district");
		model.addAttribute("pager", this.savingAccountDepositService
				.savingAccountDepoistPaginateBySavingAccountId(id, pager));
		model.addAttribute("beneficiary", beneficiary);
		model.addAttribute("savingAccount", savingAccount);
		return "/microfinance/savingAccountDeposits/list";
	}

	@RequestMapping(value = "/microfinance/savingAccounts/{id}/savingAccountDeposits/add")
	public String add(Model model) {
		return "/microfinance/savingAccountDeposits/add";
	}
}
